﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// StartPage.xaml 的交互逻辑
        /// </summary>
        public partial class ChinaMapPage : Page
        {
                private readonly GlobalManager gm = GlobalManager.Instance;

                private readonly List<Polygon> polygons = new List<Polygon>();


                public ChinaMapPage()
                {
                        InitializeComponent();
                }

                private void Page_Loaded(object sender, RoutedEventArgs e)
                {
                        FindVisualChild(CanvasPolygonPointer);

                        foreach (Polygon polygon in polygons)
                        {
                                polygon.MouseLeftButtonDown += Polygon_MouseLeftButtonDown;
                        }
                }

                private void Polygon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                {
                        gm.Province = (sender as Polygon).Tag.ToString();
                        NavigationService.Navigate(new Uri("ManageCorporatesPage.xaml", UriKind.Relative));
                }

                /// <summary>
                /// WPF查找元素的子元素
                /// </summary>
                /// <param name="obj"></param>
                /// <returns></returns>
                private void FindVisualChild(DependencyObject obj)
                {
                        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                        {
                                dynamic child = VisualTreeHelper.GetChild(obj, i);
                                if (child != null && child is Polygon)
                                {
                                        polygons.Add(child as Polygon);
                                }
                        }
                }
        }
}